package com.wskh.oss.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.wskh.commonUtil.R;
import com.wskh.oss.service.OssService;
import com.wskh.oss.service.VideoService;
import com.wskh.oss.util.InitObject;
import com.wskh.oss.util.VodKey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(value = "阿里云文件管理")
@RestController
@RequestMapping("/eduOss/fileOss")
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE}) // 允许请求跨域
public class OssController {

    @Autowired
    private OssService ossService;
    @Autowired
    private VideoService videoService;

    // 上传头像
    @ApiOperation(value = "讲师头像上传")
    @PostMapping("uploadAvatar")
    public R uploadAvatar(
            @ApiParam(name = "file", value = "头像", required = true)
            @RequestParam("file") MultipartFile file) throws IOException {
        String uploadUrl = ossService.uploadAvatar(file);
        //返回r对象
        return R.ok().message("讲师头像上传成功").data("url", uploadUrl);
    }

    @PostMapping("oss/{id}")
    public R testOss(@PathVariable String id) throws IOException {
        //返回r对象
        return R.ok().message("id = "+id);
    }


    @ApiOperation(value = "视频上传")
    @PostMapping("upload")
    public R uploadVideo(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file) throws Exception {
        System.out.println(111);
        String videoId = videoService.uploadVideo(file);
        return R.ok().message("视频上传成功").data("videoId", videoId);
    }


    @ApiOperation(value = "根据视频id删除云端视频")
    @PostMapping("deleteVideoById/{id}")
    public R deleteVideoById(@PathVariable(name = "id") String id){
        boolean b = videoService.deleteVideoById(id);
        if(b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @GetMapping("getPlayAuth/{videoId}")
    public R getVideoPlayAuth(@PathVariable("videoId") String videoId) throws
            Exception {
        //获取阿里云存储相关常量
        String accessKeyId = VodKey.accessKeyId;
        String accessKeySecret = VodKey.accessKeySecret;
        //初始化
        DefaultAcsClient client = InitObject.initVodClient(accessKeyId,
                accessKeySecret);
        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        //得到播放凭证
        String playAuth = response.getPlayAuth();
        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }
}
